/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.etsmtl.log540.tp3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author SDION
 */
public class RepeatingClient {

    final String UTF8 = "UTF8";
    protected static boolean alreadyLoaded = false;

    protected static int port;
    protected static Socket socket;

    protected static OutputStreamWriter writer;
    protected static BufferedReader reader;

    protected static byte[] utfString;
    protected static int numberOfRepetitions = 1;

    public static void main(String[] args) {
        try {
            InitializeRepeatingClient();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void InitializeRepeatingClient() throws IOException {
        if (!alreadyLoaded) {
            System.out.println("***closeServer pour fermer serveur, closeClient pour fermer client*** ");
            alreadyLoaded = true;
        }

        System.out.print("Ecrire un message : ");
        reader = new BufferedReader(new InputStreamReader(System.in));

        do {
            try {
                String inputString = reader.readLine();
                SendToServer(inputString);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtil.close(reader);
            }
        } while (reader.ready());
    }

    private static void SendToServer(String message) throws IOException {
        if (message.equals("closeClient")) {
            System.exit(1);
        }

        AskForRepetitions();

        port = 1337;
        socket = new Socket("127.0.0.1", port);

        try {
            writer = new OutputStreamWriter(socket.getOutputStream(), "UTF8");

            long startTime = System.currentTimeMillis();
            for (int i = 0; i < numberOfRepetitions; i++) {
                writer.write((i + 1) + " - " + message + "\n");
                writer.flush();
            }
            socket.close();
            long endTime = System.currentTimeMillis();

            long totalTime = endTime - startTime;
            System.out.println("Temps d'envoi : " + totalTime + " ms");

            InitializeRepeatingClient();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtil.close(socket);
            IOUtil.close(writer);
        }
    }

    private static void AskForRepetitions() throws IOException {
        System.out.print("Nombre de repetitions?: ");
        reader = new BufferedReader(new InputStreamReader(System.in));

        do {
            try {
                String repetitions = reader.readLine();
                numberOfRepetitions = Integer.parseInt(repetitions);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtil.close(reader);
            }
        } while (reader.ready());
    }
}
